<?php
/**
 * alltosun.com 影院段门店列表
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-4-28 下午12:42:50 $
 * $Id: $
 */
class Action
{
    private $per_page = 50;
    /**
     * 显示列表
     */
    public function index()
    {
        $page_no       = Request::getParam('page_no', 1);
        $status        = Request::getParam('status', -1);
        $progress      = Request::getParam('progress', '-1');
        $search_filter = Request::getParam('search_filter', array());
        $is_delete     = Request::getParam('is_delete', 1);
        $type          = Request::getParam('type', 100);
        $test          = Request::getParam('test', 0);

        extract(URLTracker::init(array(
            '$page_no' => 1,
            'status' => -1,
            'progress' => '-1',
            'search_filter' => array(),
            'is_delete' => 1,
            'type' => 100,
            'is_excel' => 0,

        )));

        // 拼装搜索条件
        $filter = array();

        //获取当前用户可看的区域
        $role_id = user_helper::get_admin_role_id();
        if (!user_helper::is_all_can_show($role_id)) {
            $filter['id'] = store_helper::get_store_ids_by_admin_user();
            if(!$filter['id']){
                $filter['id'] = -1;
            }
        }

        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }

        if ($type != -1) {
            $filter['type'] = $type;
        }

        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%' . $search_filter['title'] . '%';
        }

        if (isset($search_filter['title_branch']) && $search_filter['title_branch']) {
            $filter['`title_branch` LIKE '] = '%' . $search_filter['title_branch'] . '%';
        }

        if (isset($search_filter['dealer_name']) && $search_filter['dealer_name']) {
            $dealer_ids = user_helper::get_user_ids_by_realname($search_filter['dealer_name']);
            if (!$dealer_ids) {
                $dealer_ids = user_helper::get_user_ids_by_nickname($search_filter['dealer_name']);
            }
            if ($dealer_ids) {
                $filter['agency_id'] = $dealer_ids;
            }
        }

        if (isset($search_filter['realname']) && $search_filter['realname']) {
            $store_user_ids = user_helper::get_user_ids_by_realname($search_filter['realname']);
            if (!$store_user_ids) {
                $store_user_ids = user_helper::get_user_ids_by_nickname($search_filter['realname']);
            }
            if ($store_user_ids) {
                $filter['user_id'] = $store_user_ids;
            }
        }

        if (isset($search_filter['mobile']) && $search_filter['mobile']) {
            $store_user_id_list = user_helper::get_user_ids_by_mobile($search_filter['mobile']);
            if ($store_user_id_list) {
                if (isset($filter['user_id'])) {
                    $filter['user_id'] = array_intersect($filter['user_id'], $store_user_id_list);
                } else {
                    $filter['user_id'] = $store_user_id_list;
                }
            }
        }

        if (isset($search_filter['district_id']) && $search_filter['district_id']) {
            $filter['district_id'] = $search_filter['district_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['district_id']);
            Response::assign('area', $area);
        } elseif (isset($search_filter['city_id']) && $search_filter['city_id']) {
            $filter['city_id'] = $search_filter['city_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['city_id']);
            Response::assign('area', $area);
        } elseif (isset($search_filter['province_id']) && $search_filter['province_id']) {
            $filter['province_id'] = $search_filter['province_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['province_id']);
            Response::assign('area', $area);
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'] . ' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'] . ' 23:59:59';
        }

        if ('-1' != $status) {
            $filter['status'] = $status;
        } else {
            $filter['status >= '] = 0;
        }

        // 传-1的时候查看所有的信息
        if ($is_delete != -1) {
            $filter['is_delete'] = $is_delete;
        }

        $filter['is_enable'] = 1;
        $filter['`agency_id` !='] = 0;

        $store_list = get_data_list('store', enable_filter($filter), ' order BY `id` DESC ', $page_no, $this->per_page);

        if ($store_list) {
            $type_arr = store_config::$type_arr;
            foreach ($store_list as $k => $v) {
                $user_info = _uri('user', @$v['owner_id']);
                if ($user_info) {
                    $store_list[$k]['user_name'] = $user_info['user_name'];
                    $store_list[$k]['mobile']    = $user_info['mobile'];
                }
                if (isset($type_arr[$v['type']])) {
                    $store_list[$k]['type_name']  = $type_arr[$v['type']];
                }
                if ($v['agency_id']) {
                    $dealer_info = _uri('user', $v['agency_id']);
                    if ($dealer_info) {
                        $store_list[$k]['dealer_name'] = $dealer_info['realname'];
                        $store_list[$k]['dealer_mobile'] = $dealer_info['mobile'];
                    }
                }
            }
        }
    
        Response::assign('store_list', $store_list);
        Response::assign('status', $status);
        Response::assign('type', $type);
        Response::assign('is_delete', $is_delete);
    
        Response::assign('progress', $progress);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/movie_store_list.html');
    }
    
}
